﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CSSAdriverapp
{
    /// <summary>
    /// Interaction logic for EditEventVehicle.xaml
    /// </summary>
    public partial class EditEventVehicle : Window
    {
        private int parameter;

        public EditEventVehicle(int parameter)
        {
            InitializeComponent();
            this.parameter = parameter;
            populateGrid();
        }

        public void populateGrid()
        {
            DataClassesDataContext db = new DataClassesDataContext();
            IQueryable<Vehicle> vehicles = from c in db.Vehicles select c;
            vehicles.ToList();
            displayDataGrid.ItemsSource = vehicles;
        }

        public List<Vehicle> vehicles
        {
            get;
            set;
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            if (displayDataGrid.SelectedItem != null)
            {
                
                int eid = ((Vehicle)(displayDataGrid.SelectedItem)).ID;
                DataClassesDataContext db = new DataClassesDataContext();
                // Query the database for the row to be updated.
                var query =
                    from ev in db.Events
                    where ev.ID == parameter
                    select ev;
                // Execute the query, and change the column values
                // you want to change.
                foreach (Event ev in query)
                {
                    ev.VehicleID = eid;
                    // Insert any additional changes to column values.
                }

                // Submit the changes to the database.
                try
                {
                    db.SubmitChanges();
                    this.Close();
                }
                catch (Exception w)
                {
                    Console.WriteLine(w);
                    // Provide for exceptions.
                }
                this.Close(); 
            }
        }

    }
}
